class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int left = 0, right = 0, n = nums.size(), sum = 0;
        int ret = -1;
        for (int i = 0; i < n; i++) {
            sum += nums[i];
        }
        int target = sum - x;
        int tmp = 0;
        if (target < 0) {
            return -1;
        }
        while (right < n) {
            tmp += nums[right];
            while (tmp > target) {
                tmp -= nums[left++];
            }
            if (tmp == target) {
                ret = max(ret, right - left + 1);
            }
            right++;
        }
        if (ret == -1) return ret;
        else return n - ret;

    }
};